var timelineBlock = 1; 
var timelinePerPage = 10;
//var timelineSize = 0;
var checkLoadTimeline = false;
var timelineCommentPerPage = 2; 
$(document).ready(function() {
 	startPage();

	$( window ).resize(function() {
	  startPage();
	});	
	
	$('.search-icon').click(function(e) {
		$('#search_form').submit();
		
        e.preventDefault();
        e.stopPropagation();
    });
	
	loadTimeline();
	
    $.fn.is_on_screen = function () {
        var win = $(window);
        var viewport = {
            top: win.scrollTop(),
            left: win.scrollLeft()
        };
        viewport.right = viewport.left + win.width();
        viewport.bottom = viewport.top + win.height();

        var bounds = this.offset();
        bounds.right = bounds.left + this.outerWidth();
        bounds.bottom = bounds.top + this.outerHeight();

        return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
    };
    if ($('.timelineMore').length > 0) { 
        if ($('.timelineMore').is_on_screen()) {
	        if(checkLoadTimeline){
		        setTimeout(function(){ loadTimeline(); }, 1000);
	        }
        }
    }
    $(window).scroll(function () { 
        if ($('.timelineMore').length > 0) { 
            if ($('.timelineMore').is_on_screen()) { 
	            if(checkLoadTimeline){
		        	setTimeout(function(){ loadTimeline(); }, 1000);
	        	}
	        }
        }
    }); 

	$(window).on("orientationchange",function(){
	  activeMobile();
	  SlideMenu(0);
	});    
	
	$(document).on({
	    mouseleave: function(){
			$('.timeline-action-box').hide('blind');
	    }
    }, '.timeline-action-box');
    
	$('.action-icon1 , .action-icon2 , .action-icon3').click(function(e) {
	    $(this).find('.action-icon-staus').show('blind');
		
        e.preventDefault();
        e.stopPropagation();
    });	
	
	$(document).on({
	    mouseleave: function(){
			$(this).hide('blind');
	    }
    }, '.action-icon-staus');	
    
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
		$('.container').css('min-width',1000);
	}

    $(".colorboxopen").colorbox({rel:'colorboxopen'});
    
    if('.titleTab_knowledge2'.length > 0){
	    $('.main_menu').height($('body').height());
    }
		
});
function startPage(){
	var main_menu = $(window).height();
	$('.main_menu').css('min-height',main_menu);
	var search_box = $('.main-logo').height();
	$('.search-box').height(search_box-4);
	$('.search-icon').height(search_box-2);
}

function openComment(id){
	//demo
	var box = ".timeLineBox[data-id='" + id +"']";
	$(box+' .Comment_show').hide();
	var findmax = $(box+' .Comment_tab_id').length;
	var findopen = $(box+' .Comment_tab_id:visible').length;
	var endOpen = findopen + timelineCommentPerPage;
	
	for(var i = findopen;i < endOpen; i++ ){
		$(box+' .Comment_tab_id').eq(i).fadeIn();
	}
	
	var findclose = $(box+' .Comment_tab_id:hidden').length;
	if(findclose == 0){
		$(box+' .Comment_show_more').fadeOut();
	}else{
		$(box+' .Comment_show_more').fadeIn();
	}
	masonry();
}
function SlideMenu(id){
	if(id == 1){
		$( ".main_menu" ).addClass( "main_menu_slide" );
	}else{
		$( ".main_menu" ).removeClass( "main_menu_slide" );
	}
}
function activeMobile(){
	var buttonopen = $('.mobile_menu:visible').length;
	if(buttonopen == 0){
		var documentheight = $(document).height();
		$('.main_menu').css({'max-height':documentheight , 'min-height':documentheight});
	}else{
		$('.main_menu').css({'max-height':'none' , 'min-height':'none'});
	}
}
function masonry(){				  
	  var $container = $('.timeLineZone').isotope({
	    itemSelector: '.timeLineBox'
	  });

	  setTimeout(function(){ boxPosition(); }, 400);			  
}
function boxPosition(){

					$( ".timeLineZone" ).append( "<div class=\"clear\"></div>");
					var myHeight = $('.body_content').height();
					$('.main_menu').height(myHeight+20);
					var myHeight = $('.timeLineZone').height();
					$('.center-line').height(myHeight+20);
					$('.timelineMore').css('top',myHeight+20).fadeIn(1000);
					checkLoadTimeline = true;
}
function openActionBox(id){
	$('.timeline-action-box').hide(function() {
		var box = ".timeLineBox[data-id='" + id +"']";
			$(box+' .timeline-action-box').show('blind');
    });
	
}
function colorboxopen(href){
	$.colorbox({
	    href: href,
	    fastIframe: false
	});
}
function openMoreBox(id){
	$(".timeLineBox[data-id='" + id +"'] .knowledge-Detail p").show('blind',function() {
    	masonry();
  	});
	$(".timeLineBox[data-id='" + id +"'] .knowledge-more").hide();
}

function loadTimeline(){
		var step = 0;
		var limitPage = timelinePerPage-1;
		$( ".timeLineZone div" ).remove('.clear');
		checkLoadTimeline = false;
		$('.timeLineZone .timeLineBox:hidden').each(function(i, item) {
		    var blockid = $(this).attr('data-id');
		    
			$.get( "knowledge_content.html", { id: blockid })
				.done(function( data ) {		    
			    	$(".timeLineBox[data-id='" + blockid +"']").html(data).show().find("time.timeago").timeago();
			    	var countRow = $(".timeLineBox[data-id='" + blockid +"'] .knowledge-Detail").find('p').length;
			    	if(countRow > 1){
				    	$(".timeLineBox[data-id='" + blockid +"'] .knowledge-more").show();
			    	}
			    	
			    	if(step == limitPage){
				    	masonry();
				    	
			    	}else{
			    		step++;
			    	}
			    });	
		    if(i >= limitPage){
		    	return false;
		    }
		});

}